import time
import redis
import MySQL
import const
import faker
import hashlib

def md5(s,salt="sdgsg#@$"):
    new_s = str(s) + salt
    print(new_s)
    m = hashlib.md5(new_s.encode())
    return m.hexdigest()


'''
CREATE TABLE fmz_user(
id int auto_increment PRIMARY KEY,
email VARCHAR(32) NOT NULL UNIQUE,
phone VARCHAR(11) NOT NULL UNIQUE,
password VARCHAR(32) NOT NULL
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE fmz_account(
id int auto_increment PRIMARY KEY,
user_id int NOT NULL UNIQUE,
money int NOT NULL DEFAULT 100
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
'''
def create_user(number):
    f=faker.Faker(locale='zh_CN')
    m=MySQL.MySQL(const.mysql_info)
    password='123456'
    for i in range(number):
        i=i+1
        email = f.free_email()
        phone = f.phone_number()
        new_passwd=md5(password)
        insert_user_sql='insert into fmz_user (email,phone,password) values ("%s","%s","%s");'%(email,phone,new_passwd)
        insert_account_sql='insert into fmz_account (user_id) value (%s);'%i
        m.execute(insert_user_sql)
        m.execute(insert_account_sql)
    print("数据创建成功")

def create_redis():
    return redis.Redis(**const.redis_info,decode_responses=True)

def create_token(username):
    token = "%s%s" %(username,time.time())
    return md5(token)

def check_token(token):
    r = create_redis()
    ret = r.get("token:%s"%token)
    return ret


def check_money(money):
    try:
        int(money)
    except:
        return False
    if int(money)>0:
        return int(money)



if __name__ == '__main__':
    create_user(30)